package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.ScreenJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.dao.model.ScreenForm;
import cn.sciento.farm.devicemanager.service.ScreenService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.sciento.farm.devicemanager.common.util.ReturnUtil.getReturnMap;


@Service
@AutoJsonRpcServiceImpl
public class ScreenJsonApiImpl implements ScreenJsonApi {

    @Autowired
    private ScreenService screenService;

    @Override
    public ResponseMessage create(ScreenForm screen) {
        if(StringUtils.isEmpty(screen.getEui() )
                || StringUtils.isEmpty(screen.getName() )
                || screen.getDeviceIds() == null
                || StringUtils.isEmpty(screen.getTitle())){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        if( screen.getDeviceIds().size() >4){
            return getReturnMap(ResultConstant.SCREEN_EXCEED_DEVICE);
        }
        int count = screenService.create(screen);
        return getReturnMap(ResultConstant.SUCCESS,count,screen);
    }

    @Override
    public ResponseMessage update(ScreenForm screen) {
        if(StringUtils.isEmpty(screen.getEui() )
                || StringUtils.isEmpty(screen.getName() )
                || StringUtils.isEmpty(screen.getTitle())){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        if(screen.getDeviceIds()!= null && screen.getDeviceIds().size() >4){
            return getReturnMap(ResultConstant.SCREEN_EXCEED_DEVICE);
        }
        int count = screenService.updateById(screen);
        return getReturnMap(ResultConstant.SUCCESS,count,screen);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = screenService.deleteById(id);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage list(Long areaId) {
        List<ScreenForm> list = screenService.getByAreaId(areaId);
        return getReturnMap(ResultConstant.SUCCESS,list);
    }

    @Override
    public ResponseMessage get(Long id) {
        ScreenForm screenForm = screenService.getById(id);
        return getReturnMap(ResultConstant.SUCCESS,screenForm);
    }
}
